/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import io.sentry.util.StringUtils;

import java.io.IOException;
import java.util.Locale;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

/**
 * SentryEnvelopeItemHeaderAdapter
 *
 * @since 2021-06-02
 */
@ApiStatus.Internal
public final class SentryEnvelopeItemHeaderAdapter extends TypeAdapter<SentryEnvelopeItemHeader> {
    @Override
    public void write(JsonWriter writer, SentryEnvelopeItemHeader value) throws IOException {
        if (value == null) {
            writer.nullValue();
            return;
        }
        writer.beginObject();

        if (value.getContentType() != null) {
            writer.name("content_type");
            writer.value(value.getContentType());
        }

        if (value.getFileName() != null) {
            writer.name("filename");
            writer.value(value.getFileName());
        }

        if (!SentryItemType.Unknown.equals(value.getType())) {
            writer.name("type");
            writer.value(value.getType().getItemType().toLowerCase(Locale.ROOT));
        }

        writer.name("length");
        writer.value(value.getLength());

        writer.endObject();
    }

    @Override
    public @Nullable SentryEnvelopeItemHeader read(JsonReader reader) throws IOException {
        if (reader.peek() == JsonToken.NULL) {
            reader.nextNull();
            return null;
        }

        String contentType = null;
        String fileName = null;
        SentryItemType type = SentryItemType.Unknown;
        int length = 0;

        reader.beginObject();
        while (reader.hasNext()) {
            switch (reader.nextName()) {
                case "content_type":
                    contentType = reader.nextString();
                    break;
                case "filename":
                    fileName = reader.nextString();
                    break;
                case "type":
                    try {
                        final String nextString = StringUtils.capitalize(reader.nextString());
                        if (nextString != null) {
                            type = SentryItemType.valueOf(nextString);
                        }
                    } catch (IllegalArgumentException ignored) {
                        // invalid type
                    }
                    break;
                case "length":
                    length = reader.nextInt();
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();

        return new SentryEnvelopeItemHeader(type, length, contentType, fileName);
    }
}
